package com.fruit.service.push.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fruit.common.jgpush.PushUtils;
import com.fruit.common.mipush.MiPushUtils;
import com.fruit.service.push.PushService;
import com.xiaomi.push.sdk.ErrorCode;
import com.xiaomi.xmpush.server.Result;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.PushPayload;
/**
 * 系统Push通知
 * @author Administrator
 *
 */
@Service("pushService")
public class PushServiceImpl implements PushService{
	protected Logger logger = LoggerFactory.getLogger(PushServiceImpl.class);
	@Override
	public void sendToAllRegId(String regId, String alert, String title,
			Map<String, String> extras) {
		logger.info("【消息】---【params】---regId={}---alert={}---title={}---extras={}",regId,alert,title,extras.toString());
		PushPayload payload=null;
		//极光
		try {
			payload=PushUtils.buildPushWithRegistrationId(regId, alert, title, extras);
			PushResult result =PushUtils.sendPush(payload);
			logger.info("Got Jpush result - " + result);
			if (result.getResponseCode() == 200) { // 状态码等于200 为成功
				logger.info("Got Jpush result -success ");
			}
		}catch (APIConnectionException e) {
			logger.error("[Push] Connection error. ", e);
			logger.error("Sendno: " + payload.getSendno());

		} catch (APIRequestException e) {
			logger.error("[Push] Error response from JPush server. ", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
			logger.info("Msg ID: " + e.getMsgId());
		} catch (Exception e) {
			logger.error("[Push] Error send message to JPush server. ", e);
		}
		//小米
		try {
			 Result result=MiPushUtils.sendMessageToRegId(title, title, alert, regId, 0, 0);
			 logger.info("Got Mipush result - " + result);
			 if(ErrorCode.Success==result.getErrorCode()){
				 logger.info("Got Mipush result -success ");
			 }else{
				 logger.info("Msg ID: " + result.getMessageId());
				 logger.info("Error Code: " + result.getErrorCode());
				 logger.info("Error Message: " + result.getReason());
			 }
		} catch (Exception e) {
			logger.error("[Push] Error send message to MiPush server. ", e);
		}
		
		
		logger.info("【消息】---【结束】--");
	}

	@Override
	public void sendToAll_All(String alert, String title, Map<String, String> extras) {
		logger.info("【消息】---【params】---alert={}---title={}---extras={}",alert,title,extras.toString());
		PushPayload payload=null;
		//极光
		try {
			payload=PushUtils.buildPushObject_android_and_ios(alert, title, extras);
			PushResult result =PushUtils.sendPush(payload);
			logger.info("Got result - " + result);
			if (result.getResponseCode() == 200) { // 状态码等于200 为成功
				 logger.info("Got Jpush result -success ");
			}
		}catch (APIConnectionException e) {
			logger.error("[Push] Connection error. ", e);
			logger.error("Sendno: " + payload.getSendno());

		} catch (APIRequestException e) {
			logger.error("[Push] Error response from JPush server. ", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
			logger.info("Msg ID: " + e.getMsgId());
		} catch (Exception e) {
			logger.error("[Push] Error send message to JPush server. ", e);
		}
		//小米
		try {
			 Result result=MiPushUtils.sendBroadcastAll(title, title, alert, 0, 0);
			 logger.info("Got Mipush result - " + result);
			 if(ErrorCode.Success==result.getErrorCode()){
				 logger.info("Got Mipush result -success ");
			 }else{
				 logger.info("Msg ID: " + result.getMessageId());
				 logger.info("Error Code: " + result.getErrorCode());
				 logger.info("Error Message: " + result.getReason());
			 }
		} catch (Exception e) {
			logger.error("[Push] Error send message to MiPush server. ", e);
		}
		logger.info("【消息】---【结束】--");
	}

	@Override
	public void sendToAll_Alias(String[] alias,String alert,String title) {
		logger.info("【消息】---【params】---alias={}---extras={}",alert);
		PushPayload payload=null;
		//极光
		try {
			payload=PushUtils.buildPushObject_all_alias_alert(alias, alert);
			PushResult result =PushUtils.sendPush(payload);
			logger.info("Got result - " + result);
			if (result.getResponseCode() == 200) { // 状态码等于200 为成功
				 logger.info("Got Jpush result -success ");
			}
		}catch (APIConnectionException e) {
			logger.error("[Push] Connection error. ", e);
			logger.error("Sendno: " + payload.getSendno());

		} catch (APIRequestException e) {
			logger.error("[Push] Error response from JPush server. ", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
			logger.info("Msg ID: " + e.getMsgId());
		} catch (Exception e) {
			logger.error("[Push] Error send message to JPush server. ", e);
		}
		//小米
		try {
			 Result result=MiPushUtils.sendMessageToAlias(title, title, alert, alias, 0, 0);
			 logger.info("Got Mipush result - " + result);
			 if(ErrorCode.Success==result.getErrorCode()){
				 logger.info("Got Mipush result -success ");
			 }else{
				 logger.info("Msg ID: " + result.getMessageId());
				 logger.info("Error Code: " + result.getErrorCode());
				 logger.info("Error Message: " + result.getReason());
			 }
		} catch (Exception e) {
			logger.error("[Push] Error send message to MiPush server. ", e);
		}
		logger.info("【消息】---【结束】--");
	}
	

}
